package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.comon.UserHolder;
import com.itheima.reggie.domain.AddressBook;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 七安
 * @description 针对表【address_book(地址管理)】的数据库操作Service实现
 * @createDate 2022-08-15 16:15:17
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook>
        implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    //保存地址
    @Override
    public void saveAddress(AddressBook addressBook) {
        //根据用户登陆信息添加
        addressBook.setUserId(UserHolder.get().getId());
        //根据用户id查询是否有设置过默认地址
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, UserHolder.get().getId());
        wrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook address = addressBookMapper.selectOne(wrapper);
        //如果为null
        if (address == null) {
            //设置为默认地址
            addressBook.setIsDefault(1);
        }
        //添加地址
        addressBookMapper.insert(addressBook);
    }

    //删除地址
    @Override
    public void deleteAddress(List<Long> ids) {
        //查询要删除的地址里是否有默认地址
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault, 1);
        wrapper.in(AddressBook::getId, ids);
        AddressBook addressBook = addressBookMapper.selectOne(wrapper);
        //如果有的话将默认地址改为其他地址
        if (addressBook != null) {
            //查询所有地址
            LambdaQueryWrapper<AddressBook> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(AddressBook::getIsDefault, 0);
            wrapper1.last("limit 1");
            AddressBook book = addressBookMapper.selectOne(wrapper1);
            if (book != null) {
                book.setIsDefault(1);
                //更新数据库
                addressBookMapper.updateById(book);
            }
        }
        //删除
        addressBookMapper.deleteBatchIds(ids);
    }

    @Override
    public AddressBook findDefault() {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, UserHolder.get().getId())
                .eq(AddressBook::getIsDefault, 1);
        return addressBookMapper.selectOne(wrapper);
    }

    //修改默认地址
    @Override
    public void updateAddress(Long id) {
        //查询当前用户所有的地址
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, UserHolder.get().getId());
        List<AddressBook> addressBookList = addressBookMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(addressBookList)) {
            for (AddressBook addressBook : addressBookList) {
                // 0为非默认
                addressBook.setIsDefault(0);
                //更新数据库
                addressBookMapper.updateById(addressBook);
            }
        }
        //根据传来的id修改默认地址
        AddressBook addressBook = addressBookMapper.selectById(id);
        //  1为默认地址
        addressBook.setIsDefault(1);
        addressBookMapper.updateById(addressBook);

    }

}




